/*
 * Package il.ac.biu.cs.grossmm.api.activeData
 * File CacheTest.java
 * 
 * This is a part of presence service framework prototype. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.impl.activeData;

import static org.junit.Assert.*;

import il.ac.biu.cs.grossmm.api.OperationFailedException;

import java.util.Random;

import org.junit.Before;
import org.junit.Test;

/**
 * Juint tests for the <tt>Cache</tt> class
 *
 */
public class CacheTest {
	Cache<Integer, Integer> emptyCache;

	Retriever retriever;

	static class Retriever implements DataRetriever<Integer, Integer> {
		int magic = 12345678;

		int delay = 0;

		Random r = new Random();

		public Integer retrieve(Integer key) {
			if (delay > 0)
				try {
					Thread.sleep(r.nextInt(delay));
				} catch (InterruptedException e) {
				}

			return transform(key);
		}

		Integer transform(Integer n) {
			return n ^ magic;
		}
	}

	@Before
	public void setUp() {
		retriever = new Retriever();
		emptyCache = new Cache<Integer, Integer>(retriever, 100);
	}

	@Test
	public void testGet() throws OperationFailedException {
		Cache<Integer, Integer> cache = emptyCache;

		assertEquals(retriever.transform(123), cache.get(123));
		assertEquals(retriever.transform(123), cache.get(123));

		for (int i = 0; i < 1000; i++) {
			for (int j = 0; j < i % 4; j++)
				assertEquals(retriever.transform(i), cache.get(i));
		}

		assertEquals(100, cache.queue.size());
	}

	@Test
	public void testUpdateK() throws OperationFailedException {
		Cache<Integer, Integer> cache = emptyCache;

		cache.get(123);

		cache.update(123);
		assertEquals(retriever.transform(123), cache.get(123));

		retriever.magic = 951951;
		cache.update(123);
		assertEquals(retriever.transform(123), cache.get(123));

		cache.update(231);
		assertEquals(retriever.transform(231), cache.get(231));

		retriever.magic = 195195;

		cache.update(231);
		assertEquals(retriever.transform(231), cache.get(231));

		cache.update(123);
		assertEquals(retriever.transform(123), cache.get(123));

		cache.update(321);
		assertEquals(retriever.transform(321), cache.get(321));

		cache.update(321);
		assertEquals(retriever.transform(321), cache.get(321));

		assertEquals(3, cache.queue.size());

		for (int i = 0; i < 1000; i++) {
			if (i % 2 == 0)
				cache.get(i);
			cache.update(123);
			assertEquals(retriever.transform(i), cache.get(i));
		}

		assertEquals(100, cache.queue.size());
	}

	@Test
	public void testUpdateKV() throws OperationFailedException {
		Cache<Integer, Integer> cache = emptyCache;

		for (int i = 0; i < 1000; i++) {
			cache.get(i);
			int val = i ^ 12345;
			cache.update(i, val);
			assertEquals(val, cache.get(i));
		}

		assertEquals(100, cache.queue.size());
	}

	@Test
	public void testRemove() throws OperationFailedException {
		Cache<Integer, Integer> cache = emptyCache;

		cache.remove(123);

		assertEquals(0, cache.queue.size());

		cache.get(123);

		retriever.magic = 9812;

		cache.remove(123);

		assertEquals(retriever.transform(123), cache.get(123));
	}

	@Test
	public void concurrencyTest() {
		final Random r = new Random();
		final Cache<Integer, Integer> cache = emptyCache;
		retriever.delay = 50;

		class Worker extends Thread {
			public void run() {
				try {
					for (int i = 0; i < 200; i++) {
						int n = r.nextInt(200);
						assertEquals(retriever.transform(n), cache.get(n));

						if (r.nextBoolean())
							if (r.nextBoolean())
								cache.remove(r.nextInt(200));
							else
								cache.update(r.nextInt(200));
					}
				} catch (Exception e) {
					e.printStackTrace();
					fail(e.getMessage());
				}
			}
		}

		// use 20 threads
		Worker[] workers = new Worker[20];

		// create/start all
		for (int i = 0; i < workers.length; i++) {
			workers[i] = new Worker();
			workers[i].start();
		}

		// wait untill all threads terminated
		try {
			for (int i = 0; i < workers.length; i++) {
				workers[i].join();
			}
		} catch (InterruptedException e) {
		}
	}

}
